package com.student.education.controller;

import com.student.education.common.CommonPage;
import com.student.education.common.CommonResult;
import com.student.education.entity.ExamScore;
import com.student.education.service.ExamScoreService;
import com.student.education.to.CheckWorkTo;
import com.student.education.to.ExamScoreQueryTo;
import com.student.education.to.ExamScoreTo;
import com.student.education.util.ExcelUtils;
import com.student.education.vo.ExamScoreQueryVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 考试成绩表(ExamScore)表控制层
 *
 * @author makejava
 * @since 2021-10-04 09:45:14
 */
@RestController
@RequestMapping("/score")
public class ExamScoreController {
    /**
     * 服务对象
     */
    @Resource
    private ExamScoreService examScoreService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ExamScore selectOne(Integer id) {
        return this.examScoreService.queryById(id);
    }


    @PostMapping("/queryMyStudent")
    public CommonResult<CommonPage<ExamScoreQueryTo>> queryMyStudent(@RequestBody ExamScoreQueryVo vo) {
        return CommonResult.success(examScoreService.queryMyStudent(vo));
    }

    /**
     * Excel导入
     * @param file
     */
    @PostMapping("/upload")
    public CommonResult<String> readExcel(@RequestBody MultipartFile file){
        System.out.println("上传的文件："+file.getOriginalFilename());
        List<ExamScoreTo> list = ExcelUtils.readExcel("", ExamScoreTo.class, file);
        int total = examScoreService.insert(list);
        if (total>0){
            CommonResult.success("导入成功");
        }
        return CommonResult.error("导入失败");
    }
    /**
     * 模板下载
     * @param response
     */
    @GetMapping("/downloadModel")
    public void downloadModel(HttpServletRequest request, HttpServletResponse response){
        List<ExamScoreTo> empList = new ArrayList<>();
        ExcelUtils.writeExcel(request,response, empList, ExamScoreTo.class);
        return;
    }

}